﻿using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MongoDBTest3_Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new MongoClient("mongodb://127.0.0.1");
            var db = client.GetDatabase("TestDB2");

            var person = db.GetCollection<Person>("Person");

            #region 插入测试数据
            //List<Person> list = new List<Person>()
            //{
            //    new Person()
            //    {
            //      Age=1,
            //      CreatedAtUtc=DateTime.UtcNow,
            //      FavoriteNames=new HashSet<string>(){ "看书","音乐","运动" },
            //      Name="张三",
            //      FavoriteNumbers=new int[]{ 5,6 }, //最喜欢的数字
            //      Pets=new List<Pet>()
            //      {
            //          new Pet(){ Name="小白" },
            //           new Pet(){ Name="小黑" }
            //      }
            //    },
            //    new Person()
            //    {
            //      Age=2,
            //      CreatedAtUtc=DateTime.UtcNow,
            //      FavoriteNames=new HashSet<string>(){ "读书","摄影","登山" },
            //      Name="李四",
            //      FavoriteNumbers=new int[]{ 8,6 }, //最喜欢的数字
            //      Pets=new List<Pet>()
            //      {
            //          new Pet(){ Name="小哈" },
            //           new Pet(){ Name="多肉" }
            //      }
            //    },
            //    new Person()
            //    {
            //      Age=3,
            //      CreatedAtUtc=DateTime.UtcNow,
            //      FavoriteNames=new HashSet<string>(){ "棋牌","游戏","画画" },
            //      Name="王五",
            //      FavoriteNumbers=new int[]{ 3,4 }, //最喜欢的数字
            //      Pets=new List<Pet>()
            //      {
            //          new Pet(){ Name="二哈" },
            //           new Pet(){ Name="三哈" }
            //      }
            //    },
            //    new Person()
            //    {
            //      Age=4,
            //      CreatedAtUtc=DateTime.UtcNow,
            //      FavoriteNames=new HashSet<string>(){ "aa","bb","cc" },
            //      Name="赵柳",
            //      FavoriteNumbers=new int[]{ 6,7,8 }, //最喜欢的数字
            //      Pets=new List<Pet>()
            //      {
            //          new Pet(){ Name="四哈" },
            //           new Pet(){ Name="无哈" }
            //      }
            //    },
            //    new Person()
            //    {
            //      Age=5,
            //      CreatedAtUtc=DateTime.UtcNow,
            //      FavoriteNames=new HashSet<string>(){ "dd","ee","ff" },
            //      Name="田七",
            //      FavoriteNumbers=new int[]{ 10,666 }, //最喜欢的数字
            //      Pets=new List<Pet>()
            //      {
            //          new Pet(){ Name="六哈" },
            //           new Pet(){ Name="七哈" }
            //      }
            //    }
            //};


            //person.InsertMany(list);
            #endregion


            #region 排序和分页
            //取前2条数据
            //OrderBy：按升序对序列的元素进行排序。
            //OrderByDescending：按降序对序列的元素排序。
            //ThenBy：按升序对序列中的元素执行后续排序。
            //ThenByDescending：按降序对序列中的元素执行后续排序。
            //这里的排序是:age desc,name asc
            //var list=person.AsQueryable().OrderByDescending(p=>p.Age).ThenBy(p=>p.Name).Skip(1).Take(2).ToList();
            //foreach (var item in list)
            //{
            //    Console.WriteLine(item.Name + "," + item.Age);
            //}
            #endregion


            #region SelectMany
            ////取出所有宠物,也可用SelectMany
            //var query = from p in person.AsQueryable()
            //            from pet in p.Pets
            //            select pet;

            //foreach (var pet in query.ToList())
            //{
            //    Console.WriteLine(pet.Name);
            //}

            //Console.WriteLine("============================================");

            //var query2 = person.AsQueryable().SelectMany(p => p.Pets);

            //foreach (var pet in query2.ToList())
            //{
            //    Console.WriteLine(pet.Name);
            //}
            #endregion


            #region Linq分组和表达式分组
            //var query = from p in person.AsQueryable()
            //            group p by p.Name into g
            //            select new { Name = g.Key, Count = g.Count(), Sum = g.Sum(a => a.Age), Avg = g.Average(a => a.Age), Max = g.Max(a => a.Age), Min = g.Min(a => a.Age) };

            //foreach (var item in query.ToList())
            //{
            //    Console.WriteLine($"Name:{item.Name},Count:{item.Count},Sum:{item.Sum},Avg:{item.Avg},Max:{item.Max},Min:{item.Min}");
            //}

            //Console.WriteLine("=========================================================================");

            //person.AsQueryable().GroupBy(p => p.Name, (k, s) => new { Name = k, Count = s.Count(), Sum = s.Sum(a => a.Age), Avg = s.Average(a => a.Age), Max = s.Max(a => a.Age), Min = s.Min(a => a.Age) });

            //foreach (var item in query.ToList())
            //{
            //    Console.WriteLine($"Name:{item.Name},Count:{item.Count},Sum:{item.Sum},Avg:{item.Avg},Max:{item.Max},Min:{item.Min}");
            //}

            #endregion


            Console.WriteLine("\n ok");
        }
    }
}
